package org.jiuling.aliyun.sms.factory.service.impl;

import com.aliyun.dysmsapi20170525.models.AddShortUrlRequest;
import com.aliyun.dysmsapi20170525.models.DeleteShortUrlRequest;
import com.aliyun.dysmsapi20170525.models.QueryShortUrlRequest;
import org.jiuling.aliyun.sms.config.AliYunSmsConfig;
import org.jiuling.aliyun.sms.convert.AddShortUrlResponseConvert;
import org.jiuling.aliyun.sms.convert.DeleteShortUrlResponseConvert;
import org.jiuling.aliyun.sms.convert.QueryShortUrlResponseConvert;
import org.jiuling.aliyun.sms.exception.AliYunSMSRuntimeException;
import org.jiuling.aliyun.sms.factory.AliYunSmsSynchronization;
import org.jiuling.aliyun.sms.factory.service.AliYunShortChainManagementService;
import org.jiuling.aliyun.sms.factory.service.verify.AliYunShortChainManagementVerify;
import org.jiuling.aliyun.sms.model.AddShortUrlResponseModel;
import org.jiuling.aliyun.sms.model.DeleteShortUrlResponseModel;
import org.jiuling.aliyun.sms.model.QueryShortUrlResponseModel;

/**
 * 阿里云同步短链接管理Service
 * @author 紫灵
 * @date 2023年02月21日 16时15分19秒
 */
public class AliYunSyncShortChainManagementServiceImpl extends AliYunSmsSynchronization implements AliYunShortChainManagementService {

    public AliYunSyncShortChainManagementServiceImpl(AliYunSmsConfig aliYunSmsConfig) {
        super(aliYunSmsConfig);
    }

    @Override
    public AddShortUrlResponseModel addShortUrl(String sourceUrl, String shortUrlName, int effectiveDays) {
        AliYunShortChainManagementVerify.addShortUrlParam(sourceUrl, shortUrlName, effectiveDays);
        AddShortUrlRequest addShortUrlRequest = new AddShortUrlRequest();
        addShortUrlRequest.setEffectiveDays(String.valueOf(effectiveDays));
        addShortUrlRequest.setShortUrlName(shortUrlName);
        addShortUrlRequest.setSourceUrl(sourceUrl);
        try {
            return AddShortUrlResponseConvert.bodyToModel(create().addShortUrl(addShortUrlRequest).getBody());
        } catch (Exception e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public DeleteShortUrlResponseModel deleteShortUrl(String sourceUrl) {
        AliYunShortChainManagementVerify.deleteShortUrlParam(sourceUrl);
        DeleteShortUrlRequest deleteShortUrlRequest = new DeleteShortUrlRequest();
        deleteShortUrlRequest.setSourceUrl(sourceUrl);
        try {
            return DeleteShortUrlResponseConvert.bodyToModel(create().deleteShortUrl(deleteShortUrlRequest).getBody());
        } catch (Exception e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }

    @Override
    public QueryShortUrlResponseModel queryShortUrl(String shortUrl) {
        AliYunShortChainManagementVerify.queryShortUrlParam(shortUrl);
        QueryShortUrlRequest queryShortUrlRequest = new QueryShortUrlRequest();
        queryShortUrlRequest.setShortUrl(shortUrl);
        try {
            return QueryShortUrlResponseConvert.bodyToModel(create().queryShortUrl(queryShortUrlRequest).getBody());
        } catch (Exception e) {
            throw new AliYunSMSRuntimeException(e.getLocalizedMessage());
        }
    }
}
